package org.example.controller;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.example.domain.ResponseResult;
import org.example.domain.dto.CategoryListDto;
import org.example.domain.entity.Category;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.service.CategoryService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @PostMapping("/categoryList")
    public ResponseResult categoryList(@RequestBody CategoryListDto categoryListDto){

        return categoryService.categoryList(categoryListDto);
    }

    @PostMapping("/addCategory")
    public ResponseResult addCategory(@RequestBody Category category){
        if(StringUtils.isBlank(category.getName())){
            throw new SystemException(AppHttpCodeEnum.NOT_ARTICLE_TITLE_ERROR);
        }
        return categoryService.addCategory(category);
    }

    @PutMapping("/updateCategory")
    public ResponseResult updateCategory(@RequestBody Category category){
        if(StringUtils.isBlank(category.getName())){
            throw new SystemException(AppHttpCodeEnum.NOT_ARTICLE_TITLE_ERROR);
        }
        return categoryService.updateCategory(category);
    }

    @GetMapping("/categories")
    public ResponseResult categories(){
        return categoryService.categories();
    }
}
